﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;

namespace NewsReaderApp.Helpers
{
    public sealed class BooleanToVisibilityConverter : IValueConverter
    {
        public BooleanToVisibilityConverter()
        {
            TrueValue = Visibility.Visible;
            FalseValue = Visibility.Collapsed;
        }

        public Visibility TrueValue
        {
            get;
            set;
        }

        public Visibility FalseValue
        {
            get;
            set;
        }

        public object Convert(Object value, Type type, Object parameter, String language)
        {
            if (value is bool)
            {
                var boolValue = (bool)value;
                return boolValue ? TrueValue : FalseValue;
            }

            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            if (value is Visibility)
            {
                var vis = (Visibility)value;
                return vis == TrueValue;
            }

            return false;
        }
    }

    public sealed class NullToVisibilityConverter : IValueConverter
    {
        public NullToVisibilityConverter()
        {
            NullValue = Visibility.Collapsed;
            NonNullValue = Visibility.Visible;
        }

        public Visibility NullValue
        {
            get;
            set;
        }

        public Visibility NonNullValue
        {
            get;
            set;
        }

        public object Convert(Object value, Type type, Object parameter, String language)
        {
            return value == null ? NullValue : NonNullValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotSupportedException();
        }
    }
    
    public sealed class DateFormatter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            DateTime dt;
            if (value is DateTime)
            {
                dt = (DateTime)value;
            }
            else if (value is DateTime? && value != null)
            {
                dt = ((DateTime?)value).Value;
            }
            else if (value is DateTimeOffset)
            {
                dt = ((DateTimeOffset)value).LocalDateTime;
            }
            else if (value is DateTimeOffset? && value != null)
            {
                dt = ((DateTimeOffset?)value).Value.LocalDateTime;
            }

            else
            {
                return null;
            }

            if (parameter == null)
            {
                return dt.ToString();
            }
            else
            {
                return dt.ToString(parameter as string);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    
    public sealed class CategoryToColorConverter : IValueConverter
    {
        private static readonly Dictionary<string, Color> s_catColors = new Dictionary<string, Color>()
        {
            {"Top News", Colors.AliceBlue},
            {"US", Colors.Azure},
            {"World", Colors.Blue},
            {"Business", Colors.Orange},
            {"Technology", Colors.DeepSkyBlue},
            {"Science", Colors.Green},
            {"Health", Colors.White},
            {"Justice", Colors.Red},
            {"Politics", Colors.Brown},
            {"Most Popular", Colors.YellowGreen},
            {"Travel", Colors.LightGreen},
            {"Entertainment", Colors.Yellow},
            {"Sports", Colors.DarkGreen}
        };


        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string catName = value as string;
            if (catName == null)
                return null;

            Color c;
            if (!s_catColors.TryGetValue(catName, out c))
            {
                c = Colors.Transparent;
            }

            if (parameter != null)
            {
                double tranparency;
                if (double.TryParse(parameter as string, NumberStyles.Any, CultureInfo.InvariantCulture, out tranparency))
                {
                    c = Color.FromArgb((byte)(255.0 * tranparency / 100), c.R, c.G, c.B);
                }
            }

            if (targetType == typeof(Color))
            {
                return c;
            }
            else if (targetType == typeof(Brush))
            {
                return new SolidColorBrush(c);
            }
            return null;
        }
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotSupportedException();
        }
    }
}
